setwd("/home/uec-00/yapingli/code/ASM-poster")
normalCpg<-array()
tumorCpg<-array()
normalCpgX<-array()
tumorCpgX<-array()
for(k in c("normal")){
	for(s in c(1:22)){
		fileName=paste("methylCGsRich_ASM_AllSnp_",k,"Merge_chr",s,"_table.txt",sep="")
		x<-read.table(fileName,header=F,sep="\t")
		i=1
		while(i<=length(x[,2])){
			temp<-x[i,1]
			y<-x[x[,1]==temp,]
			len<-length(y[,2])
			
			minRange<-min(y[,2])
			maxRange<-max(y[,2])
			region=paste(minRange,maxRange,sep="~")
			
			A<-sum(y[,3:4])
			B<-sum(y[,5:6])
			C<-sum(y[,3:6])
			thisRow<-cbind(s,temp,region,length(y[,2]),A,B,C)
			normalCpg<-rbind(normalCpg,thisRow)

			i<-i+len
		}
		
	}
}
outputName=paste("methylCGsRich_ASM_AllSnp_normalMerge_CpG_blocks.txt",sep="")
write.table(normalCpg, outputName,sep="\t",quote=F,col.names=F,row.names=F)

for(k in c("tumor")){
	for(s in c(1:22)){
		fileName=paste("methylCGsRich_ASM_AllSnp_",k,"Merge_chr",s,"_table.txt",sep="")
		x<-read.table(fileName,header=F,sep="\t")
		i=1
		while(i<=length(x[,2])){
			temp<-x[i,1]
			y<-x[x[,1]==temp,]
			len<-length(y[,2])
			
			minRange<-min(y[,2])
			maxRange<-max(y[,2])
			region=paste(minRange,maxRange,sep="~")
			
			A<-sum(y[,3:4])
			B<-sum(y[,5:6])
			C<-sum(y[,3:6])
			thisRow<-cbind(s,temp,region,length(y[,2]),A,B,C)
			tumorCpg<-rbind(tumorCpg,thisRow)

			i<-i+len
		}
		
	}
}

outputName=paste("methylCGsRich_ASM_AllSnp_tumorMerge_CpG_blocks.txt",sep="")
write.table(tumorCpg, outputName,sep="\t",quote=F,col.names=F,row.names=F)
